<?php

namespace App\Http\Controllers\Api;

use App\Models\Sms;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class SmsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    public function sendsms(request $request){

        $message = [
            'mobile.required' => '请输入手机号',
            'mobile.regex' => '请输入正确的手机号',
            'event.required' => '您因什么事件发送短信',
        ];
        $validator = Validator::make($request -> toArray(),[
            'mobile' => ['required','regex:/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/'],
            'event' => 'required'
        ], $message);

        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }


        //存入数据库
        if($sms = Sms::store($request -> mobile,$request -> event)){

            if(isset($sms['error'])){

                return jsonError($sms['error']);
            }

            return jsonSuccess([],'',200,2002);
        }else{
            return jsonError(4005);
        }
    }


}
